#!/bin/bash

cd /home/ludovic/ns7pl

# Functions
function quit {
   if [ -e $pl_zip_file ]
   then
		echo "Cleaning up pl.xml.zip file...";
		rm $pl_zip_file;
   fi
   echo "All done. Bye bye."
   exit;
}

# Constants
pl_zip_file="symantec_v2.pl.xml.zip"
pl_unzipped_file="symantec_v2.pl.xml"
pl_url="http://www.solutionsam.com/solutions/pl/symantec_v2.pl.xml.zip"

# Record execution time
date > lastruntime

# pl_url="http://localhost/symantec.pl.xml" #Local file for testing

# Check if the symantec.pl.xml file is already in current directory
if [ -e $pl_zip_file ]
then
   rm $pl_zip_file
fi
if [ -e $pl_unzipped_file ]
then
   rm $pl_unzipped_file
fi

# Download the product list xml (zipped)
echo "Downloading product listing now...";
err=`wget $pl_url -q`; # -q for quiet

if [ -e $pl_zip_file ]
then
   echo "... product listing ZIP downloaded!" #> /dev/null;
else
   echo "... product listing ZIP download failed!!!";
   quit;
fi

unzip "$pl_zip_file" > /dev/null;

# Verify the MD5
echo "Checking the product list md5";
prodList_md5=`md5sum $pl_unzipped_file | sed 's/  symantec_v2.pl.xml//g'`;

echo "Downloaded pl md5 ='$prodList_md5'";

# Get current pl xml md5
current_md5=`md5sum symantec.pl.xml | sed 's/  symantec.pl.xml//g'`;

# Did the product list xml change?
if [ "$prodList_md5" == "$current_md5" ]
then
   echo "File with hash $prodList_md5 already exists. Nothing to do!";
   quit;
else
   echo "We have downloaded a new file. Move it to the historical name.";
   mv $pl_unzipped_file symantec.pl.xml;

   ./create-solnsam-mirror_amd64 -l 1 -f symantec.pl.xml
   # GIT stuff comes here :D
   git add solutions
   git add symantec.pl.xml
   git commit -m "Symantec.pl.xml md5 = $prodList_md5"

fi

